package com.jnu.myaccount.function.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jnu.myaccount.R;
import com.jnu.myaccount.data.Data;
import com.jnu.myaccount.data.DataBank.DataBank;
import com.jnu.myaccount.data.Money;
import com.jnu.myaccount.data.Person;
import com.jnu.myaccount.databinding.FragmentUserBinding;
import com.jnu.myaccount.function.sort.SortFragment;
import com.jnu.myaccount.record.IncomeFragment;
import com.jnu.myaccount.record.PaidFragment;



import java.util.ArrayList;

public class UserFragment extends Fragment implements View.OnClickListener {
    private UserViewModel userViewModel;
    private FragmentUserBinding binding;
    private int all_record;//记录的总条数
    private int all_sort;//分类记录的总数
    private int all_paid;//支出总条数
    private int all_income;//收入总条数
    private EditText eName;
    private EditText eQianming;
    private TextView tRecord;
    private TextView tSort;
    private TextView tDetail;
    private TextView tPaid;
    private TextView tAll;
    private TextView tIncome;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {


        binding = FragmentUserBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
       eName=root.findViewById(R.id.name);//名字
        eQianming=root.findViewById(R.id.qianming);//签名
        tRecord=root.findViewById(R.id.all_record_num);//记录总
        tSort=root.findViewById(R.id.all_sort_num);//总分类
        tDetail=root.findViewById(R.id.all_detail);//总收支
        tPaid=root.findViewById(R.id.now_allpaid);//下框支出
        tAll=root.findViewById(R.id.now_allmoney);//下框总收出
        tIncome=root.findViewById(R.id.now_allIncome);//下框收入
        TextView tv_OK=root.findViewById(R.id.OK);
        tv_OK.setOnClickListener(this);
        //展示用于姓名，签名
        DataBank mDataBank=new DataBank(getActivity());
        Person user=mDataBank.loadUser();
        if(user==null){
            Person mPerson=new Person("My account","我的记账本");
            mDataBank.saveUser(mPerson);
        }
        else{
            eName.setText(user.getUserName());
            eQianming.setText(user.getQianming());
        }

        //展示记录总条数
        if(mDataBank.loadData()!=null){
            ArrayList<Data> mList=mDataBank.loadData();
            all_record=mList.size();
            tRecord.setText("共有"+all_record+"条");
           all_sort=0;

            for(int i=0;i< PaidFragment.itemNames.length;i++){
                for(int j=0;j<mList.size();j++)
                {
                    if(PaidFragment.itemNames[i].equals(mList.get(j).itemName)){
                        all_sort++;
                        continue;
                    }
                }
            }
            for(int i = 0; i< IncomeFragment.itemNames_Income.length; i++){
                for(int j=0;j<mList.size();j++)
                {
                    if(IncomeFragment.itemNames_Income[i].equals(mList.get(j).itemName)){
                        all_sort++;
                        continue;
                    }
                }
            }
            tSort.setText("共计"+all_sort+"种分类");

            all_paid=0;
            for(int k=0;k<mList.size();k++)
            {
                if(mList.get(k).itemCost<=0)
                    all_paid++;
            }
            all_income=0;
            for(int l=0;l<mList.size();l++)
            {
                if(mList.get(l).itemCost>0)
                    all_income++;
            }
            tDetail.setText("支出"+all_paid+"条 收入"+all_income+"条");

            for(int k=0;k<mList.size();k++)
            {
                if(mList.get(k).itemCost<=0)
                    all_paid++;
            }

            Money money=mDataBank.loadMoney();
            if(money!=null){
                tAll.setText(money.getAllMoney()+"元");
                tPaid.setText(money.getAllPaid()+"元");
                tIncome.setText(money.getAllEarn()+"元");
            }
        }

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.OK:
                Person newUser=new Person(eName.getText().toString(),eQianming.getText().toString());
                DataBank mDataBank=new DataBank(getActivity());
                mDataBank.saveUser(newUser);
                Toast.makeText(getActivity(),"更改成功",Toast.LENGTH_SHORT).show();

                break;
        }

    }
}